package gui;

import image.Image;
import processing.core.PApplet;
import processing.core.PImage;
import ataques.Ataque1;
import ataques.Ataque2;
import ataques.BaseAtaque;
import cifrado.CifradoActual;
import controlP5.Button;
import controlP5.ControlEvent;
import controlP5.ControlP5;
import controlP5.Textfield;
import descifrado.Descifrado;

public class BaseGui extends PApplet {
	
	ControlP5 cip;
	
	Textfield fileNameInput;

	Image lena;
	CifradoActual ca;
	Descifrado dc;
	BaseAtaque att1,att2;
	String fileName = "../pics/asdf.bmp";

	int ox = 20, oy = 80, o=20, bw = 100,bh = 20;
	
	PImage C,M,Y,K;
	PImage dith;
	PImage origi;
	PImage desc;
	PImage a1cm, a1my, a1cy ,a2;
	

	LoadCanvas loadCanvas;
	CifradoCanvas cifradoCanvas;
	DescifradoCanvas descifradoCanvas;
	AtaqueCanvas ataqueCanvas;
	
	private static final long serialVersionUID = -8775600128782965344L;

	@Override
	public void setup() {
		this.size(500,500);
		this.frameRate(25);

		
		cip = new ControlP5(this);

		cip.tab("default").setLabel("cargar");
		cip.addTab("cifrado");
		cip.addTab("descifrado");
		cip.addTab("ataques");
		
		fileNameInput = cip.addTextfield("file name", ox, 2*o, 2*bw, bh);
		fileNameInput.setFocus(true);
		fileNameInput.setText(this.fileName);
		cip.controller("file name").moveTo("default");
		cip.addButton("cargar",0,ox,4*o,bw,bh);
		cip.controller("cargar").moveTo("default");
		
		this.loadMatrices(this.fileName);
		

	}
	
	public void controlEvent(ControlEvent theEvent) {
		if(theEvent.controller() instanceof Textfield) {
			println(theEvent.controller().name());
			this.fileName = theEvent.controller().stringValue();
			this.loadMatrices(this.fileName);
		}
		else if (theEvent.controller() instanceof Button) {
			println(theEvent.controller().name());
			this.fileName = fileNameInput.stringValue();
			this.loadMatrices(this.fileName);
		} 
	}
	
	private void loadMatrices(String url) {
		lena = new Image(url);
		origi = lena.getOriginal();
		ca = new CifradoActual(lena.getImageWidth(),lena.getImageHeight(),lena.getDithered());
		
		dith = lena.getPImageFromDithered(lena.getDithered());
		
		K = lena.getPImageFromBoolean(ca.getK());
		C = lena.getPImageFromBoolean(ca.getC());
		M = lena.getPImageFromBoolean(ca.getM());
		Y = lena.getPImageFromBoolean(ca.getY());
		
		dc = new Descifrado(ca.getC(),ca.getM(),ca.getY(),ca.getK());
		desc = lena.getPImageFromDithered(dc.getImgSuperimposed());

		att1 = new Ataque1(ca.getC().length, ca.getC()[0].length, ca.getC(),ca.getM());
		a1cm = lena.getPImageFromDithered(att1.getDesc()); // FIXME
		att1 = new Ataque1(ca.getC().length, ca.getC()[0].length, ca.getM(),ca.getY());
		a1my = lena.getPImageFromDithered(att1.getDesc()); // FIXME
		att1 = new Ataque1(ca.getC().length, ca.getC()[0].length, ca.getC(),ca.getY());
		a1cy = lena.getPImageFromDithered(att1.getDesc()); // FIXME


		att2 = new Ataque2(ca.getC().length, ca.getC()[0].length, ca.getC(),ca.getM(), ca.getY(), ca.getK());
		a2 = lena.getPImageFromDithered(att2.getDesc());

		cip.tab("default").removeCanvas(loadCanvas);
		cip.tab("cifrado").removeCanvas(cifradoCanvas);
		cip.tab("descifrado").removeCanvas(descifradoCanvas);
		cip.tab("ataques").removeCanvas(ataqueCanvas);
		
		loadCanvas = new LoadCanvas(origi, dith);
		cifradoCanvas = new CifradoCanvas(C,M,Y,K);
		descifradoCanvas = new DescifradoCanvas(desc);
		ataqueCanvas = new AtaqueCanvas(a1cm, a1my, a1cy, a2);
		
		cip.tab("default").addCanvas(loadCanvas);
		cip.tab("cifrado").addCanvas(cifradoCanvas);
		cip.tab("descifrado").addCanvas(descifradoCanvas);
		cip.tab("ataques").addCanvas(ataqueCanvas);
	}
	
	
	public void draw() {
		this.background(50);
		
		
	}
	

	public void submit(int theValue) {
	  fileNameInput.submit();
	}
	
	public static void main(String[] args) {
		BaseGui gui = new BaseGui();
		
		gui.setup();
		while (true) {
			gui.draw();
		}
		
	}
}
